// 小程序
const getAuth = (auth = 'userLocation') => {
    return new Promise((resolve, reject) => {
        uni.getSetting({
            success(res) {
                if (!res.authSetting[`scope.${auth}`]) {
                    uni.authorize({
                        scope: `scope.${auth}`,
                        success() {
                            resolve()
                        },
                        async fail(e) {
                            uni.hideLoading()
                            await showText(auth, resolve, reject)
                        }
                    })
                } else {
                    resolve()
                }
            }
        })
    })
}

const showText = async (auth, resolve, reject) => {
    await uni.showModal({
        title: '提示',
        content: '请前往设置打开权限',
        success(res) {
            if (res.confirm) {
                uni.openSetting({
                    async success(res) {
                        console.log(res, res.authSetting[`scope.${auth}`])
                        if (!res.authSetting[`scope.${auth}`]) {
                            return await showText(auth, resolve, reject)
                        } else {
                            return resolve()
                        }

                    },
                    fail(err) {
                        console.log(err)
                        return reject()
                    }
                })
            } else if (res.cancel) {
                uni.showModal({
                    title: '提示',
                    content: '请授权小程序权限，\n「右上角」-「设置」中开启',
                    showCancel: false,
                    success(res) {
                        if (res.confirm) {
                            return reject()
                        }
                    }
                })
            }
        }
    })
}


function getlocation() {
    return new Promise((resolve,reject)=>{
        getAuth().then(res => {
            console.log('授权成功')
            uni.getLocation({
                type: 'wgs84',
                success: (r) => {
                    console.log('定位成功')
                    const {
                        latitude,
                        longitude
                    } = r
                    uni.setStorageSync('latitude', latitude)
                    uni.setStorageSync('longitude', longitude)
                    resolve({
                        latitude,
                        longitude
                    }) 
                },
                fail: err => {
                    console.log('定位失败')
                   resolve({
                        latitude: uni.getStorageSync('latitude') || 0,
                        longitude: uni.getStorageSync('longitude') || 0
                    }) 
                }
            })
        }).catch(() => {
            console.log('授权失败')
            resolve({
                 latitude: uni.getStorageSync('latitude') || 0,
                 longitude: uni.getStorageSync('longitude') || 0
             }) 
        })
    })
}

export default getlocation
